<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use  Validator;
use  Auth;
use  Hash;
class PasswordRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
       public function guize(){

        Validator::extend('check_password',function ($attribute, $value, $parameters, $validator){
            return Hash::check($value,Auth::user()->password);
        });
       }
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        //手动调用一下自定义的验证规则方法
         $this->guize();
        //nullable如果使用验证方法，如果没有填，后面的验证就不会使用验证规则
        //如果填写，那么后面的验证必须满足验证规则
        return [
             'name'=>'required',
             'oldpassword' => 'nullable|check_password',
             'password' => 'nullable|confirmed|between:6,20',
             'password_confirmation' => 'nullable|between:6,20',
        ];
    }
    public function messages()
    {
        return[
            'oldpassword.check_password' => '旧密码输入错误!!',
        ];
    }
}
